<html>
<#include "../common/header.ftl" />

<!--引入jquery和wangEditor.js-->
<!--注意：javascript必须放在body最后，否则可能会出现问题-->
<link rel="stylesheet" type="text/css"
	href="static/dist/css/wangEditor.min.css">
<link rel="stylesheet" type="text/css" href="static/css/select.css">
<script type="text/javascript" src="static/dist/js/wangEditor.min.js"></script>
<script type="text/javascript" src="static/js/select.js"></script>
</head>
<script type="text/javascript">
	$(function() {
		$('#subBtn').on('click', function() {
			if (($("#ip1").val().trim()) == '') {
				tip('标题不可为空');
				return;
			}
			if (($("#textarea1").val().trim()) == '') {
				tip('内容不可为空！');
				return;
			}
			let obj = {};
			obj.title = $("#ip1").val().trim();
			obj.type = parseInt($("#ip2").val().trim());
			obj.content = $("#textarea1").val().trim();
			obj.img = $("#ip3").val().trim();
			console.log(JSON.parse(JSON.stringify(obj)))
			$.ajax({
				url : "indexnews?method=<#if info ??>edit&id=${info.id}<#else>add</#if>",
				type : "POST",
				data : JSON.stringify(obj),
				scriptCharset : 'utf-8',
				contentType : "application/json; charset=utf-8",
				success : function(res) {
					console.log(JSON.parse(res))
					let data = JSON.parse(res)
					if (data.code === 0) {
						tip(data.msg)
						<#if info ??>
						setTimeout(" location.reload()",  700)
						<#else>
						setTimeout(" location.reload()",  700)
						</#if>
						
					} else {
						tip(data.msg)
					}
				},
				error : function(res) {
					console.log("res", res);
				}
			});
			/* console.log('data', $('form').serializeArray()) */
		})
	});
	function tip(msg) {
		$("#warn").text(msg)
		$("#modal-282051").click()
	}
	function del(id){
		$.ajax({
			url : "indexnews?method=delete&id="+id,
			type : "GET",
			success : function(res) {
				let data = JSON.parse(res)
				if (data.code === 0) {
					tip(data.msg)
					setTimeout(" location.reload()",  700)
					}
				else{
					tip(data.msg)
				}
			}
		})
	}
</script>
<#if Session.user ?? && user.role==1>
<body style="margin-left: 200px">

	<div id="wrapper" class="toggled">
		<#--边栏sidebar--> <#include "../common/nav.ftl" />
		<#--主要内容content-->
	</div>
	<#else>
	<body>
</#if>
<div class="container">

	<div class="row clearfix">

		<div class="col-md-12 column">

			<#include "../common/bread.ftl"/>
			<#include "../modal/tips.ftl"/>
			<a id="modal-282051" href="#modal-warns" style="display: none"
				role="button" class="" data-toggle="modal"></a>

			<#if types ??>
					<button class="btn btn-warning" type="button">
						<em class="glyphicon glyphicon-align-left"></em> 模块：
					</button>
				
					<#list types as item> <a href="indexnews?method=type&key=${item.type }&doer=list"><button
							class="btn btn-info" type="button">${item.name }</button></a> </#list>
					</#if>
					<hr/>
					<#if type ??>
					<div class="alert alert-success alert-dismissable">
						 <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
						<h4>
								<b>当前模块位置：<label>${type.name}</label></b>
						</h4> 
					</div>
						</#if>
					
					<#if list ??>
					
						<div class="row clearfix">
						<#list list as item>
							<div class="col-sm-4 col-md-3 col-xs-6 column">
							<div class="panel panel-primary">
							  <div class="panel-heading">
							  <blockquote>${item.title}
							  <br/>
							  <br/>
							  <div style="display:block-inline">
							  <a class="btn btn-warning" href="indexnews?method=type&key=<#if type ??>${type.type}</#if>&doer=list&id=${item.id}" >修改</a>
							  <a class="btn btn-warning" href="#" onclick="del(${item.id})" style="float:right" >删除</a>
							  </div>
							
							  
							  </blockquote>
							  </div>
							  <div class="panel-footer">${item.content}</div>
							</div>
							</div>
					</#list>
						</div>
						<hr/>
					</#if>
					
						<#if type ?? >
						
						<div class="alert 
						<#if info ??>alert-warning <#else>alert-success </#if>
						alert-dismissable">
						 <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
						<h3>	
						<#if info ??>修改新闻<#else>添加新闻至${type.name}</#if>
						</h3> 
					</div>
				<form class="container" 
					style="width: 90%; margin: 0 auto; max-width: 1380px" role="form">
					
					<div class="form-group">
						<label>新闻名称</label> <input id="ip1" class="form-control"
							placeholder="请输入..." type="text" <#if info ??>value="${info.title}" </#if> />
					</div>
					<input id="ip2" type="hidden" value="${type.type}" />
					<div class="form-group">
						<label>新闻图片</label> <input id="ip3" class="form-control"
							placeholder="请输入...url" type="text" name="img" <#if info ??><#if info.img ??>value="${info.img}" </#if></#if> />
					</div>
				
					<div class="form-group">
						<label>新闻内容</label>
						<textarea name="newsContent" id="textarea1" style="height: 500px"
							placeholder="请输入..."  ><#if info ??>  ${info.content }</#if></textarea>
						<script type="text/javascript">
							var editor = new wangEditor('textarea1');
							editor.create();
						</script>

					</div>
					<input id="ip3" type="hidden" value='<#if Session.user ??> ${Session.user.id } </#if>' />
					<button id="subBtn" type="button" class="btn btn-<#if info ??>warning<#else>primary </#if>
					">
						<#if info ??>修改 <#else>添加 </#if>
					</button>
				</form>
				</#if>
			</div>
		<script src="static/js/3.1.0bootstrap.min.js"></script>
		</body>
</html>

